#!/usr/bin/env python3
import re
import sys
import json
from pathlib import Path

def extract_log_data(log_file):
    """从日志文件中提取请求和响应数据"""
    request_data = []
    response_data = []
    
    with open(log_file, 'r', encoding='utf-8') as f:
        current_line = f.readline()
        while current_line:
            # 处理请求报文（可能跨越多行）
            if '[解密后]请求报文' in current_line:
                # 提取requestId
                request_id_match = re.search(r'requestId:\[(\d+)\]', current_line)
                if not request_id_match:
                    current_line = f.readline()
                    continue
                
                request_id = request_id_match.group(1)
                
                # 查找JSON开始位置
                json_start = current_line.find('{')
                if json_start == -1:
                    current_line = f.readline()
                    continue
                
                # 收集多行JSON内容
                json_content = []
                json_line = current_line[json_start:].strip()
                json_content.append(json_line)
                
                # 继续读取直到找到完整的JSON
                while '}' not in json_line and not current_line == '':
                    current_line = f.readline()
                    if not current_line:
                        break
                    json_line = current_line.strip()
                    json_content.append(json_line)
                
                full_json = ''.join(json_content)
                
                # 验证JSON格式
                try:
                    json.loads(full_json)
                    request_data.append(f"{request_id},{full_json}")
                except json.JSONDecodeError:
                    print(f"无效的JSON格式，requestId: {request_id}")
            
            # 处理响应报文（通常单行）
            elif '[加密前]响应报文' in current_line:
                match = re.search(r'requestId:\[(\d+)\].*?\[加密前\]响应报文:\s*(\{.*?\})', current_line)
                if match:
                    response_data.append(f"{match.group(1)},{match.group(2)}")
            
            current_line = f.readline()
    
    return request_data, response_data

def write_output_files(request_data, response_data):
    """将提取的数据写入文件"""
    with open('request_data.txt', 'w', encoding='utf-8') as f:
        f.write('\n'.join(request_data))
    
    with open('response_data.txt', 'w', encoding='utf-8') as f:
        f.write('\n'.join(response_data))

def main():
    if len(sys.argv) != 2:
        print("使用方法: python3 extract_log.py <日志文件>")
        print("示例: python3 extract_log.py nohup.out")
        sys.exit(1)
    
    log_file = sys.argv[1]
    
    if not Path(log_file).exists():
        print(f"错误: 文件 {log_file} 不存在")
        sys.exit(1)
    
    print(f"正在处理日志文件: {log_file}")
    request_data, response_data = extract_log_data(log_file)
    write_output_files(request_data, response_data)
    
    print("数据处理完成:")
    print(f"- 请求数据已保存到 request_data.txt (共 {len(request_data)} 条)")
    print(f"- 响应数据已保存到 response_data.txt (共 {len(response_data)} 条)")

if __name__ == "__main__":
    main()
